{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "07d3a81f",
   "metadata": {},
   "source": [
    "# 实践一：\n",
    "\n",
    "使用pandas读取data.csv文件，获取表格中所有值为“未知”的行，并删除所在行，保存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1c6415b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd #导入pandas库，并将其简称为pd\n",
    "df=pd.read_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",encoding=\"UTF-8\")\n",
    "drop_list=[] #创建一个空列表drop_list，用于存储需要删除的行索引\n",
    "for i in range(len(df)): # 开始一个循环，迭代遍历DataFrame中的每一行。len(df)返回的是DataFrame中的行数\n",
    "    for j in range(len(df.columns)): #在外层循环内部开始另一个循环，迭代遍历当前行中的每一个列。len(df.columns)返回的是DataFrame中的列数\n",
    "        data=df.iloc[i,j] #使用iloc方法来获取位于第i行第j列的数据\n",
    "        if data==\"未知\":\n",
    "            drop_list.append(i) #如果data确实等于“未知”，那么就把当前行的索引i添加到drop_list列表中\n",
    "df=df.drop(drop_list)   \n",
    "df.to_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",index=False) #使用to_csv方法将清洗后的DataFrame保存回原来的CSV文件路径"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b109ca",
   "metadata": {},
   "source": [
    "# 实践二：\n",
    "\n",
    "使用pandas读取data.csv文件，获取16列的所有值，并获取空值所在行，将空值替换为0，保存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "47715733",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df=pd.read_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",encoding=\"UTF-8\")\n",
    "\n",
    "data=df.iloc[: ,16] # 选择DataFrame中所有行但仅第16列,存储在变量data中\n",
    "\n",
    "#pd.isnull(data)\n",
    "pd.isna(data) # 检测data中的缺失值\n",
    "\n",
    "for i in range(len(df)):\n",
    "    is_null=pd.isna(data)[i]\n",
    "    if is_null:\n",
    "        df.iloc[i,16]=0\n",
    "df.to_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac91c2d3",
   "metadata": {},
   "source": [
    "# 实践三：\n",
    "\n",
    "使用pandas读取data.csv文件，获取所有重复行，删除所有重复行，保存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "40dcb0d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [url, id, Lng, Lat, Cid, tradeTime, DOM, followers, totalPrice, price, square, livingRoom, drawingRoom, kitchen, bathRoom, floor, buildingType, constructionTime, renovationCondition\n",
      ", buildingStructure\n",
      ", ladderRatio\n",
      ", elevator\n",
      ", fiveYearsProperty\n",
      "\n",
      ", subway\n",
      ", district, communityAverage]\n",
      "Index: []\n",
      "\n",
      "[0 rows x 26 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df=pd.read_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",encoding=\"UTF-8\")\n",
    "# 使用duplicated()方法检测DataFrame中的所有完全重复的行\n",
    "duplicates = df[df.duplicated()]\n",
    "# 使用drop_duplicates()方法删除DataFrame中的重复行\n",
    "df = df.drop_duplicates(keep='first')\n",
    "# 再次调用duplicated()来检查删除了重复行之后是否还有新的重复行\n",
    "duplicates = df[df.duplicated()]\n",
    "\n",
    "print(duplicates)\n",
    "\n",
    "df.to_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c8eeef7",
   "metadata": {},
   "source": [
    "# 实践四：\n",
    "\n",
    "使用pandas读取data.csv文件，将square列的数据精确到小数点后一位，将totalPrice列的数据精确度小数点后零位，存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f6b0bd50",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df=pd.read_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",encoding=\"UTF-8\")\n",
    "# 对DataFrame中的square列执行四舍五入操作，round(1)表示保留一位小数。\n",
    "df['square']=df['square'].round(1)\n",
    "\n",
    "df['totalPrice']=df['totalPrice'].round(1)\n",
    "\n",
    "df.to_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7993ffd2",
   "metadata": {},
   "source": [
    "# 实践五：\n",
    "\n",
    "使用pandas读取data.csv文件，将表格按照totalPrice列降序排列，price列升序排列，square列降序排列，保存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "654fdaf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df=pd.read_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",encoding=\"UTF-8\")\n",
    "\n",
    "df.sort_values(by=['totalPrice', 'price', 'square'], ascending=[False,True, False], inplace=True)\n",
    "\n",
    "df.to_csv(\"C:/Users/Administrator/Desktop/大学资料/大三上/Web数据挖掘/10.25/data.csv\",index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9996ca4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
